﻿Namespace My

    ' The following events are available for MyApplication:
    '
    ' Startup: Raised when the application starts, before the startup form is created.
    ' Shutdown: Raised after all application forms are closed.  This event is not raised if the application terminates abnormally.
    ' UnhandledException: Raised if the application encounters an unhandled exception.
    ' StartupNextInstance: Raised when launching a single-instance application and the application is already active.
    ' NetworkAvailabilityChanged: Raised when the network connection is connected or disconnected.
    Partial Friend Class MyApplication

        Private Sub MyApplication_NetworkAvailabilityChanged(sender As Object, e As Devices.NetworkAvailableEventArgs) Handles Me.NetworkAvailabilityChanged
            If e.IsNetworkAvailable AndAlso CommandLineArgs.Contains("-offline") Then
                Dim TempFile As String = My.Application.Info.DirectoryPath & "\DownloadModule.exe"
                My.Computer.FileSystem.WriteAllBytes(My.Application.Info.DirectoryPath & "\FileInfo.dll", My.Resources.FileInfo, False)
                My.Computer.FileSystem.WriteAllBytes(TempFile, My.Resources.DownloadModule, False)
                Process.Start(TempFile, My.Application.Info.DirectoryPath)
            End If
        End Sub

        Private Sub MyApplication_Startup(sender As Object, e As ApplicationServices.StartupEventArgs) Handles Me.Startup
            If Command.Length = 0 Then
                Dim TempFile As String = My.Application.Info.DirectoryPath & "\DownloadModule.exe"
                My.Computer.FileSystem.WriteAllBytes(My.Application.Info.DirectoryPath & "\FileInfo.dll", My.Resources.FileInfo, False)
                My.Computer.FileSystem.WriteAllBytes(TempFile, My.Resources.DownloadModule, False)
                Process.Start(TempFile, My.Application.Info.DirectoryPath)
                End
            End If
            Dim RegFilePath As String = My.Application.Info.DirectoryPath & "\GYCrack.key"
            If My.Computer.FileSystem.FileExists(RegFilePath) Then
                Try
                    RedeemCode = CInt(Val(My.Computer.FileSystem.ReadAllText(RegFilePath)))
                Catch
                    RedeemCode = 0
                End Try
            End If
            Do
                Synctime.SetWindowsClock(Synctime.GetTime)
                Dim MatchFound As Boolean = False
                For i = 0 To 29
                    If GetRedeemCode(Today - New TimeSpan(i, 0, 0, 0)) = RedeemCode Then
                        MatchFound = True
                        Exit For
                    End If
                Next
                If MatchFound Then
                    Exit Do
                Else
                    frmRedeemCode.MaskedTextBox1.Clear()
                    If frmRedeemCode.ShowDialog() = DialogResult.Cancel Then End
                    Try
                        RedeemCode = CInt(Val(frmRedeemCode.MaskedTextBox1.Text))
                    Catch
                        RedeemCode = 0
                    End Try
                End If
            Loop
            My.Computer.FileSystem.WriteAllText(RegFilePath, RedeemCode.ToString("000000"), False)
        End Sub

        Private Sub MyApplication_UnhandledException(sender As Object, e As ApplicationServices.UnhandledExceptionEventArgs) Handles Me.UnhandledException
            e.ExitApplication = False
            frmMain.Restart()
        End Sub

    End Class

End Namespace


Public Module SharedCode

    Public RedeemCode As Integer = 0

    Public Function GetRedeemCode(Arg As Date) As Integer
        Dim DateNum As Integer = CInt(Arg.Ticks \ 864000000000)
        Dim RNG As New Random(DateNum)
        For i = 0 To My.Application.Info.Version.Revision
            RNG.Next(1000000)
        Next
        Return RNG.Next(1000000)
    End Function

End Module